package com.lin.activity;

import java.util.ArrayList;
import java.util.Iterator;

import com.lin.fragments.PersonalDetailCardCallHistoryFragment;
import com.lin.fragments.PersonalDetailCardMSMAndCALLFragment;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.activity.BaseActivity;
import com.yunfeng.supercall.activity.ChatActivity;
import com.yunfeng.supercall.activity.FriendsDetailActivity;
import com.yunfeng.supercall.activity.OutCallActivity;
import com.yunfeng.supercall.activity.ShareActivity;
import com.yunfeng.supercall.bean.Constants;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.RecentCallsBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.bean.UserBean;
import com.lin.utils.Parameters;
import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.lin.utils.Session.SignupResult;
import com.lin.utils.Utils;
import com.lin.views.ListViewForPersonalCardHistoryCall;
import com.lin.views.ListViewForRecentCall;
import com.lin.views.MyFragmentAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.PinterestCircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalDetailCardActivity extends FragmentActivity implements
		Constants, OnClickListener {
	private ArrayList<RecentCallsBean> mRecentCallsBeanList;
	ImageView imageViewAvatar;
	private TextView mTextViewPhone;
	private TextView mTextViewSign;
	private TextView mTextViewAddress;
	private SuperFriendsBean mSuperFriendsBean;
	private SuperFriendsBean mOldSuperFriendsBean;

	Session mSession;
	ArrayList<RecentCallsBean> mList;
	DisplayImageOptions options;
	DisplayImageOptions options1;

	private ListViewForPersonalCardHistoryCall mListView;
	private TextView mTextViewUserName;

	private FrameLayout mFrameLayoutMore;
	private FrameLayout mFrameLayoutBack;
	private UserBean userBean;
	private ImageView imageViewSex;
	private FrameLayout mFrameLayoutInviteFriends3;
	private FrameLayout mFrameLayoutSendMessage1;
	private FrameLayout mFrameLayoutFocus2;
	private LinearLayout linearLayoutInViteAsFriend;
	private LinearLayout linearLayoutSuperCall;
	private ImageView imageViewSendMsn;
	private ImageView imageViewCall;
	private LinearLayout linearLayout;
	private FrameLayout mFrameLayoutPhone;
	private LinearLayout linearLayoutCallHistory;
	private ImageView imageView1;
	private ImageView imageView2;
	private ImageView imageView3;
	private ImageView imageView4;
	private ImageView imageView5;
	private TextView mTextViewSuperNumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		options = new DisplayImageOptions.Builder()
				.displayer(
						new PinterestCircleBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		options1 = new DisplayImageOptions.Builder()
				.displayer(
						new RoundedBitmapDisplayer(getResources()
								.getDimensionPixelSize(R.dimen.corners_avatar)))
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

		setContentView(R.layout.activity_personal_detailcard);
		imageViewSex = (ImageView) findViewById(R.id.imageview_sex);
		imageViewSendMsn = (ImageView) findViewById(R.id.imageview_send_msn);
		linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
		mListView = (ListViewForPersonalCardHistoryCall) findViewById(R.id.listview);

		imageViewAvatar = (ImageView) findViewById(R.id.imageview_avatar);
		mTextViewPhone = (TextView) findViewById(R.id.textView1);
		mTextViewAddress = (TextView) findViewById(R.id.textview_address);
		mTextViewSign = (TextView) findViewById(R.id.textview_sign);
		mTextViewUserName = (TextView) findViewById(R.id.textview_username);
		imageViewCall = (ImageView) findViewById(R.id.imageview_call);
		mTextViewSuperNumber = (TextView) findViewById(R.id.textview_phone);

		mSuperFriendsBean = getIntent().getParcelableExtra(KEY_DATA);
		mOldSuperFriendsBean = mSuperFriendsBean;
		mFrameLayoutMore = (FrameLayout) findViewById(R.id.framelayout_more);
		mFrameLayoutBack = (FrameLayout) findViewById(R.id.framelayout_back);
		mFrameLayoutPhone = (FrameLayout) findViewById(R.id.framelayout);

		imageView1 = (ImageView) findViewById(R.id.imageView1);
		imageView2 = (ImageView) findViewById(R.id.imageView2);
		imageView3 = (ImageView) findViewById(R.id.imageView3);
		imageView4 = (ImageView) findViewById(R.id.imageView4);
		imageView5 = (ImageView) findViewById(R.id.imageView5);
		mFrameLayoutSendMessage1 = (FrameLayout) findViewById(R.id.framelayout_personal1);
		mFrameLayoutFocus2 = (FrameLayout) findViewById(R.id.framelayout_personal2);
		mFrameLayoutInviteFriends3 = (FrameLayout) findViewById(R.id.framelayout_personal3);
		linearLayoutInViteAsFriend = (LinearLayout) findViewById(R.id.linearlayout_invite_as_friend);
		linearLayoutSuperCall = (LinearLayout) findViewById(R.id.linearlayout_supercall);
		linearLayoutCallHistory = (LinearLayout) findViewById(R.id.ll_call_history);

		imageViewCall.setOnClickListener(this);
		imageViewSendMsn.setOnClickListener(this);
		mFrameLayoutBack.setOnClickListener(this);
		mFrameLayoutMore.setOnClickListener(this);
		mFrameLayoutFocus2.setOnClickListener(this);
		linearLayout.setOnClickListener(this);
		mFrameLayoutInviteFriends3.setOnClickListener(this);
		mFrameLayoutSendMessage1.setOnClickListener(this);
		linearLayoutSuperCall.setOnClickListener(this);
		userBean = BaseActivity.getUser(getApplicationContext());

		mSession = Session.getSession();
		mSession.init(getApplicationContext(), true);

		if (mSuperFriendsBean != null) {

			mSession.getFriendDetailNew(getApplicationContext(),
					userBean.getUserid(), mSuperFriendsBean.getUserid(),
					new ResultReceiver<SuperFriendsBean>() {

						@Override
						public void onResult(boolean last, SuperFriendsBean data) {
							// TODO Auto-generated method stub
							if (data == null)
								return;
							if (data != null) {
								mSuperFriendsBean = data;
							}

							ImageLoader.getInstance()
									.displayImage(
											Parameters.SERVER_PIC_URL
													+ data.getPhoto(),
											imageViewAvatar, options1);
							mTextViewSuperNumber.setText("超号:"
									+ data.getSuperNumber());
							if (data.getSuperNumber() != null)
								mTextViewSuperNumber.setText("超号:"
										+ data.getSuperNumber());
							if (data != null
									&& mSuperFriendsBean.getType() == 2) {
								linearLayoutInViteAsFriend
										.setVisibility(View.GONE);
								mFrameLayoutInviteFriends3
										.setVisibility(View.GONE);
							}
							try {
								if (data.getAddress() != null)
									mTextViewAddress.setText(data.getAddress()
											+ "");
							} catch (NullPointerException e) {

							}
							try {
								// mTextViewPhone.setText(data.getPhone() + "");
							} catch (NullPointerException e) {

							}
							try {
								if (data.getNote() != null)
									mTextViewSign.setText(data.getNote() + "");
							} catch (NullPointerException e) {

							}
							if (mSuperFriendsBean.getSex() == 1) {
								imageViewSex.setImageDrawable(getResources()
										.getDrawable(R.drawable.ic_male));
							} else if (mSuperFriendsBean.getSex() == 2) {
								imageViewSex.setImageDrawable(getResources()
										.getDrawable(R.drawable.ic_female));
							}
							try {
								ArrayList<String> imgList = data.getmListImg();
								if (imgList.size() == 1) {
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(0),
											imageView1, options1);
								} else if (imgList.size() == 2) {
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(0),
											imageView1, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(1),
											imageView2, options1);
								} else if (imgList.size() == 3) {
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(0),
											imageView1, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(1),
											imageView2, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(2),
											imageView3, options1);
								} else if (imgList.size() == 4) {
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(0),
											imageView1, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(1),
											imageView2, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(2),
											imageView3, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(3),
											imageView4, options1);
								} else if (imgList.size() >= 5) {
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(0),
											imageView1, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(1),
											imageView2, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(2),
											imageView3, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(3),
											imageView4, options1);
									ImageLoader.getInstance().displayImage(
											Parameters.SERVER_PIC_URL
													+ imgList.get(4),
											imageView5, options1);
								}

							} catch (NullPointerException e) {

							}
							if (data != null) {
								if (TextUtils.isEmpty(data.getPhone())) {
									data.setPhone(mOldSuperFriendsBean
											.getPhone());
									mTextViewPhone.setText(mOldSuperFriendsBean
											.getPhone());

								}
							} else {
								mTextViewPhone.setText(mOldSuperFriendsBean
										.getPhone());
							}
						}

					});

			if (mSuperFriendsBean.getSex() == 1) {
				imageViewSex.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_male));
			} else if (mSuperFriendsBean.getSex() == 2) {
				imageViewSex.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_female));
			}
			/* (返回值:1=超友,2=关注,3=粉丝,0=陌生人) */
			mSession.checkRelation(getApplicationContext(),
					userBean.getUserid(), mSuperFriendsBean.getUserid(),
					new ResultReceiver<Integer>() {

						@Override
						public void onResult(boolean last, Integer data) {
							// TODO Auto-generated method stub

							mFrameLayoutSendMessage1
									.setVisibility(View.VISIBLE);
							mFrameLayoutFocus2.setVisibility(View.VISIBLE);
							mFrameLayoutInviteFriends3
									.setVisibility(View.VISIBLE);
							mListView.setVisibility(View.INVISIBLE);
							linearLayoutCallHistory
									.setVisibility(View.INVISIBLE);
							if (data == 0) {
								linearLayoutInViteAsFriend
										.setVisibility(View.VISIBLE);
								linearLayoutSuperCall.setVisibility(View.GONE);
								mFrameLayoutPhone.setVisibility(View.GONE);

							} else if (data == 1) {
								mListView.setVisibility(View.GONE);
								mFrameLayoutFocus2.setVisibility(View.GONE);
								mFrameLayoutInviteFriends3
										.setVisibility(View.VISIBLE);
								mFrameLayoutSendMessage1
										.setVisibility(View.VISIBLE);
								mFrameLayoutFocus2.setVisibility(View.GONE);

								linearLayoutInViteAsFriend.setClickable(false);

								linearLayoutInViteAsFriend
										.setVisibility(View.GONE);
								linearLayoutSuperCall
										.setVisibility(View.VISIBLE);
								mFrameLayoutPhone.setVisibility(View.VISIBLE);
								mListView.setVisibility(View.VISIBLE);
								linearLayoutCallHistory
										.setVisibility(View.VISIBLE);
							} else if (data == 2) {
								mFrameLayoutFocus2.setVisibility(View.GONE);
								mFrameLayoutInviteFriends3
										.setVisibility(View.VISIBLE);
								mFrameLayoutSendMessage1
										.setVisibility(View.VISIBLE);
								linearLayoutInViteAsFriend
										.setVisibility(View.VISIBLE);
								linearLayoutSuperCall.setVisibility(View.GONE);
								mFrameLayoutPhone.setVisibility(View.GONE);
							} else if (data == 3) {
								mFrameLayoutFocus2.setVisibility(View.GONE);
								mFrameLayoutInviteFriends3
										.setVisibility(View.VISIBLE);
								mFrameLayoutSendMessage1
										.setVisibility(View.VISIBLE);
								linearLayoutInViteAsFriend
										.setVisibility(View.VISIBLE);
								linearLayoutSuperCall.setVisibility(View.GONE);
								mFrameLayoutPhone.setVisibility(View.GONE);

							} else {

							}

							if (mSuperFriendsBean != null
									&& mSuperFriendsBean.getType() == 2) {
								linearLayoutInViteAsFriend
										.setVisibility(View.GONE);
								mFrameLayoutInviteFriends3
										.setVisibility(View.GONE);
								if (data == 2 || data == 1) {
									linearLayoutSuperCall
											.setVisibility(View.VISIBLE);
									mFrameLayoutInviteFriends3
											.setVisibility(View.VISIBLE);
								}
							}

						}

					});

			/*
			 * mSession.searchUser(getApplicationContext(),
			 * mSuperFriendsBean.getPhone(), new
			 * ResultReceiver<SuperFriendsBean>() {
			 * 
			 * @Override public void onResult(boolean last, SuperFriendsBean
			 * mSuperFriendsBean) { // TODO Auto-generated method stub
			 * mTextViewPhone.setText(mSuperFriendsBean.getPhone());
			 * mTextViewAddress.setText(mSuperFriendsBean.getAddress());
			 * mTextViewSign.setText(mSuperFriendsBean.getNote());
			 * mTextViewUserName.setText(mSuperFriendsBean.getNick_name()); }
			 * 
			 * });
			 */
			mSession.getCallHistory(getApplicationContext(), 0,
					new ResultReceiver<ArrayList<RecentCallsBean>>() {

						@Override
						public void onResult(boolean last,
								ArrayList<RecentCallsBean> mListRecentCalls) {
							// TODO Auto-generated method stub
							if (mListRecentCalls == null)
								return;
							mList = new ArrayList<RecentCallsBean>();
							for (Iterator<RecentCallsBean> iterator = mListRecentCalls
									.iterator(); iterator.hasNext();) {
								RecentCallsBean mModel = iterator.next();
								try {
									if (mModel.getUserid() == mSuperFriendsBean
											.getUserid()) {
										mList.add(mModel);
									}
								} catch (NullPointerException e) {

								}
							}
							mListView.setDataList(mList);

						}

					});

			ImageLoader.getInstance().displayImage(
					Parameters.SERVER_PIC_URL + mSuperFriendsBean.getPhoto(),
					imageViewAvatar, options);
			mTextViewPhone.setText(mSuperFriendsBean.getPhone());
			mTextViewAddress.setText(mSuperFriendsBean.getAddress());
			mTextViewSign.setText(mSuperFriendsBean.getNote());
			mTextViewUserName.setText(mSuperFriendsBean.getNick_name());
		} else {
			if (userBean.getSex() == 1) {
				imageViewSex.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_male));
			} else if (userBean.getSex() == 2) {
				imageViewSex.setImageDrawable(getResources().getDrawable(
						R.drawable.ic_female));
			}
			mSession.getFriendDetailNew(getApplicationContext(),
					userBean.getUserid(), userBean.getUserid(),
					new ResultReceiver<SuperFriendsBean>() {

						@Override
						public void onResult(boolean last, SuperFriendsBean data) {
							// TODO Auto-generated method stub
							if (data == null)
								return;
							mTextViewSuperNumber.setText("超号:"
									+ data.getSuperNumber());
							PersonalDetailCardActivity.this.mSuperFriendsBean = data;
							if (data.getType() == 2) {
								imageViewSex.setVisibility(View.INVISIBLE);
							}
							if (mSuperFriendsBean.getSex() == 1) {
								imageViewSex.setImageDrawable(getResources()
										.getDrawable(R.drawable.ic_male));
							} else if (mSuperFriendsBean.getSex() == 2) {
								imageViewSex.setImageDrawable(getResources()
										.getDrawable(R.drawable.ic_female));
							}
						}

					});
			/*
			 * mSession.searchUser(getApplicationContext(), userBean.getPhone(),
			 * new ResultReceiver<SuperFriendsBean>() {
			 * 
			 * @Override public void onResult(boolean last, SuperFriendsBean
			 * mSuperFriendsBean) {
			 * PersonalDetailCardActivity.this.mSuperFriendsBean =
			 * mSuperFriendsBean; // TODO Auto-generated method stub //
			 * mTextViewPhone.setText(mSuperFriendsBean.getPhone()); //
			 * mTextViewAddress.setText(mSuperFriendsBean.getAddress()); //
			 * mTextViewSign.setText(mSuperFriendsBean.getNote()); //
			 * mTextViewUserName.setText(mSuperFriendsBean.getNick_name()); }
			 * 
			 * });
			 */
			mSession.getCallHistory(getApplicationContext(), 0,
					new ResultReceiver<ArrayList<RecentCallsBean>>() {

						@Override
						public void onResult(boolean last,
								ArrayList<RecentCallsBean> mListRecentCalls) {
							// TODO Auto-generated method stub
							if (mListRecentCalls == null)
								return;
							mList = new ArrayList<RecentCallsBean>();
							for (Iterator<RecentCallsBean> iterator = mListRecentCalls
									.iterator(); iterator.hasNext();) {
								RecentCallsBean mModel = iterator.next();
								if (mModel.getUserid() == userBean.getUserid()) {
									mList.add(mModel);
								}
							}
							mListView.setDataList(mListRecentCalls);

						}

					});
			ImageLoader.getInstance().displayImage(
					Parameters.SERVER_PIC_URL + userBean.getPhoto(),
					imageViewAvatar, options);
			mTextViewPhone.setText(userBean.getPhone());
			mTextViewAddress.setText(userBean.getAddress());
			mTextViewSign.setText(userBean.getNote());
			mTextViewUserName.setText(userBean.getNick_name());

		}
		// mRecentCallsBeanList = (ArrayList<RecentCallsBean>)
		// getIntent().getSerializableExtra(KEY_DATA);

	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		if (mFrameLayoutBack == view) {
			finish();
		} else if (mFrameLayoutMore == view) {

			try {
				startActivityForResult(
						new Intent(this, FriendsDetailActivity.class)
								.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
								.putExtra(KEY_DATA, mSuperFriendsBean)
								.putExtra(
										KEY_IS_FRIEND,
										mSuperFriendsBean.getType() == ContactsBaseBean.TYPE_ENTERPRISE),
						500);
			} catch (NullPointerException e) {

			}
		} else if (mFrameLayoutFocus2 == view) {

			mSession.addGuanzhu(getApplicationContext(), userBean.getUserid(),
					mSuperFriendsBean.getUserid(),
					new ResultReceiver<SignupResult>() {

						@Override
						public void onResult(boolean last, SignupResult result) {
							// TODO Auto-generated method stub
							if (result == SignupResult.SUCCESS) {
								mFrameLayoutFocus2.setVisibility(View.GONE);
								linearLayoutSuperCall.setVisibility(View.GONE);
								if (mSuperFriendsBean.getType() == 2) {
									// mFrameLayoutPhone.setVisibility(View.VISIBLE);
									mFrameLayoutInviteFriends3
											.setVisibility(View.GONE);
								}
							}
						}

					});
		} else if (mFrameLayoutInviteFriends3 == view) {

			mSession.addFriendApp(getApplicationContext(),
					userBean.getUserid(), mSuperFriendsBean.getUserid(),
					new ResultReceiver<SignupResult>() {

						@Override
						public void onResult(boolean last, SignupResult result) {
							// TODO Auto-generated method stub
							if (result == SignupResult.SUCCESS) {
								mFrameLayoutFocus2.setVisibility(View.GONE);
								Toast.makeText(getApplicationContext(), "申请成功",
										Toast.LENGTH_LONG).show();

								if (TextUtils.isEmpty(mSuperFriendsBean
										.getPhone())) {
									mSuperFriendsBean
											.setPhone(mOldSuperFriendsBean
													.getPhone());
								}

								mSession.SendPushMsg(
										PersonalDetailCardActivity.this,
										userBean.getNick_name() + "发送好友申请", 1,
										mSuperFriendsBean.getPhone(),
										userBean.getNick_name() + "发送好友申请",
										new ResultReceiver<Integer>() {

											@Override
											public void onResult(boolean last,
													Integer args) {
												// TODO Auto-generated method
												// stub

											}

										});
							}
						}

					});
			/*
			 * mSession.changeRelation(getApplicationContext(),
			 * userBean.getUserid(), mSuperFriendsBean.getUserid(), 1, new
			 * ResultReceiver<SignupResult>() {
			 * 
			 * @Override public void onResult(boolean last, SignupResult result)
			 * { // TODO Auto-generated method stub if(result ==
			 * SignupResult.SUCCESS) {
			 * mFrameLayoutFocus2.setVisibility(View.GONE);
			 * Toast.makeText(getApplicationContext(),
			 * "申请成功",Toast.LENGTH_LONG).show(); } }
			 * 
			 * });
			 */
		} else if (mFrameLayoutSendMessage1 == view) {
			Intent intent = new Intent(this, ChatActivity.class);
			intent.putExtra(KEY_DATA, mSuperFriendsBean);
			startActivity(intent);

		} else if (linearLayoutSuperCall == view) {
			startActivity(new Intent(getApplicationContext(),
					OutCallActivity.class)
					.putExtra(KEY_DATA, mSuperFriendsBean));
		} else if (linearLayout == view) {
			Intent intent = new Intent(this, DynamicShareWallActivity.class);
			if (mSuperFriendsBean.getNick_name() != null) {
				intent.putExtra("username", mSuperFriendsBean.getNick_name());
			}
			intent.putExtra(KEY_DATA, mSuperFriendsBean.getUserid());
			intent.putExtra("avatar", mSuperFriendsBean.getPhoto());
			startActivity(intent);

		} else if (imageViewCall == view) {
			Utils.dial(mSuperFriendsBean.getPhone(),
					PersonalDetailCardActivity.this);
		} else if (imageViewSendMsn == view) {
			Utils.SendSMSTo(PersonalDetailCardActivity.this,
					mSuperFriendsBean.getPhone());
		}

	}

}